//
//  MKJNavigationViewController.m
//  RedBookTransitionDemo
//
//  Created by MKJING on 16/11/17.
//  Copyright © 2016年 MKJING. All rights reserved.
//

#import "BANavigationController.h"
#import "MKJAnimator.h"
#import "MKJSuperAnimation.h"
#import "BAAlbumCellNode.h"
#import "BAAlbumScrollCellNode.h"

// 弄一个子类继承nvc,在这里实现返回动画的代理方法
@interface BANavigationController () <UINavigationControllerDelegate,BAAlbumDataProtocol>
@property (nonatomic,strong) ASDisplayNode *nodeView;
@property (nonatomic,assign) CGRect destinationRec;
@property (nonatomic,assign) CGRect originalRec;
@property (nonatomic,assign) BOOL isPush;
@property (nonatomic,assign) id animationDelegate; // 动画的代理temp
@property (nonatomic,assign) BOOL isRight;

@end

@implementation BANavigationController



- (void)albumManager:(BAAlbumIndexManager *)manager didChangeToIndex:(NSInteger)index {
    BAAlbumCellNode *node = [BAALBUM_DATA.showingCollectNode nodeForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
    node = BAALBUM_DATA.isList ? (BAAlbumCellNode *)node : (BAAlbumScrollCellNode *)node ;
    self.nodeView = node.imageNode;
}

- (void)albumManagerDataSourceDidChanged:(BAAlbumIndexManager *)manager {
//    ASCellNode *node = [BAALBUM_DATA.showingCollectNode nodeForItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]];
//    self.nodeView = BAALBUM_DATA.isList ? (BAAlbumCellNode *)node : (BAAlbumScrollCellNode *)node ;
}


- (void)pushViewController:(UIViewController *)viewController imageView:(ASDisplayNode *)nodeView desRec:(CGRect)desRec original:(CGRect)originalRec deleagte:(id<MKJAnimatorDelegate>)delegate isRight:(BOOL)isRight
{
    self.isRight = isRight;
    self.delegate = self; //  让自身成为代理，实现下面的方法
    self.nodeView = nodeView;
    self.destinationRec = desRec;
    self.originalRec = originalRec;
    self.isPush = YES;
    self.animationDelegate = delegate; // 让动画的代理成为destinationVC
    [super pushViewController:viewController animated:YES];
}

- (void)pushViewController:(UIViewController<MKJAnimatorDelegate> *)viewController targetView:(ASDisplayNode *)nodeView {
//    CGFloat rate = kScreenWidth - 30 / imageNode.frame.size.width ;
    [super pushViewController:viewController animated:YES];

    return;
    
    CGRect descRec = CGRectMake(15, 79, kScreenWidth - 30, kScreenWidth - 30);
    CGRect originalRec;

    originalRec = [nodeView.supernode.view convertRect:nodeView.frame toView:viewController.view];
    [self pushViewController:viewController imageView:nodeView desRec:descRec original:originalRec deleagte:viewController isRight:YES];
}



-(UIViewController *)popViewControllerAnimated:(BOOL)animated{
    self.isPush = NO;
    return  [super popViewControllerAnimated:animated];
}

- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    if(!self.animationDelegate) {
        return nil;
    }
    // 普通push的时候就是小红书类型的
    MKJAnimator *animator = [[MKJAnimator alloc] init];
    animator.nodeView = self.nodeView;
    animator.destinationRec = self.destinationRec;
    animator.originalRec = self.originalRec;
    animator.isPush = self.isPush;
    animator.delegate = self.animationDelegate;
    
    // 左侧push的时候是屏幕碎裂类型的
    MKJSuperAnimation *superAnimator = [[MKJSuperAnimation alloc] init];
    
    if (self.isRight) {
        return animator;
    }
    else
    {
        return superAnimator;
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [BAALBUM_DATA setDelegate:self];
    self.navigationBar.barTintColor = RGB_255(18, 220, 206);
    self.navigationBar.tintColor = [UIColor whiteColor];;
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self addReturnButton:viewController];

    [super pushViewController:viewController animated:animated];
}

- (void)addReturnButton:(UIViewController *)vc {
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 27, 27)];
    [leftButton setImage:[UIImage imageNamed:@"goback"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    vc.navigationItem.leftBarButtonItem = leftBarButton;
}

- (void)goback {
    [self popViewControllerAnimated:YES];
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
